<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Services;

use App\Exceptions\ApiException;
use App\Format\LookofVillageFormat;
use App\Models\LookofVillage;
use Illuminate\Support\Facades\Redis;

class LookofVillageService
{
    const BANNER_REDIS_KEY = 'look_of_village_banner';

    const XCX_BANNER_REDIS_KEY = 'look_of_village_banner_xcx';

    public function getAdminList(int $page, int $pageSize, array $search)
    {
        $builder = LookofVillage::query();

        if (isset($search['address_name'])) {
            $builder->where('address_name', 'like', '%' . $search['address_name'] . '%');
        }

        $count = $builder->count();

        $lists = $builder->skip(($page - 1) * $pageSize)->take($pageSize)->get();

        return [
            'page' => $page,
            'page_size' => $pageSize,
            'count' => $count,
            'banner_url' => $this->getLookofVillageBanner(),
            'xcx_banner_url' => $this->getXcxLookofVillageBanner(),
            'list' => $lists,
        ];
    }

    public function getList()
    {
        return [
            'banner_url' => $this->getLookofVillageBanner(),
            'xcx_banner_url' => $this->getXcxLookofVillageBanner(),
            'list' => LookofVillage::query()
                ->orderBy('sort')
                ->get(),
        ];
    }

    public function getLookofVillageBanner()
    {
        $banner = Redis::get(self::BANNER_REDIS_KEY);
        if ($banner === false) {
            return '';
        }
        if (substr($banner, 0, 4) != 'http') {
            return config('app.url') . $banner;
        }
        return $banner;
    }

    public function getXcxLookofVillageBanner()
    {
        $banner = Redis::get(self::XCX_BANNER_REDIS_KEY);
        if ($banner === false) {
            return '';
        }
        if (substr($banner, 0, 4) != 'http') {
            return config('app.url') . $banner;
        }
        return $banner;
    }

    public function setBanner(string $bannerUrl)
    {
        Redis::set(self::BANNER_REDIS_KEY, $bannerUrl);
        return true;
    }

    public function setXcxBanner(string $bannerUrl)
    {
        Redis::set(self::XCX_BANNER_REDIS_KEY, $bannerUrl);
        return true;
    }

    public function delete(int $id)
    {
        LookofVillage::query()->find($id)->delete();
        return true;
    }

    public function create(LookofVillageFormat $lookofVillageFormat)
    {
        LookofVillage::query()->create($lookofVillageFormat->toArrayNotNull());
        return true;
    }

    public function update(LookofVillageFormat $lookofVillageFormat)
    {
        if (empty($lookofVillageFormat->getId())) {
            throw new ApiException('缺少参数', 1);
        }
        $look = LookofVillage::query()->find($lookofVillageFormat->getId());
        $data = $lookofVillageFormat->toArrayNotNull();
        unset($data['id']);
        $look->update($data);
        return true;
    }
}
